<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<script type="text/javascript">
			//			1. 有段文字text = "and in it he says Any damn fool could", 不改变原text的前提下， 提取第一段话a = "Any damn fool could", 第二段话b = "and", 第三段话c = "could", 注意， slice() 可以接受负数, string.slice().
					var txt = 'and in it he says Any damn fool could';
					var a = txt.slice(txt.indexOf('A'));
					var b = txt.match(/and/g)[0].toString();
					var c = txt.slice(-5);
					var arr = [a,b,c];
					console.log(arr);

			//			2. 字符串digite = "0123456789", 把前5个字符分割出来， 返回个单字符的数组, 注意， 返回数组里面的数字是字符串类型, string.split()
						var digite = "0123456789";
						var newDigite = digite.split('',5);
						console.log(newDigite);	
						console.log(typeof newDigite[0]);
			//3.已知有字符串foo=”get-element-by-id”,写一个function将其转化成驼峰表示法”getElementById”.
					foo='get-element-by-id'
					var newStr = '';
					var changeStr = function () {
						var newFoo = foo.split('-');
						for ( var i = 0; i < newFoo.length; i++ ) {
							if (i != 0){
								TEMP = newFoo[i][0];
								newStr += newFoo[i].replace(TEMP,TEMP.toUpperCase());
							}else{
								newStr += newFoo[i];
							}
						}					
						console.log(newStr);
					}();
			//4.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的有"天"的下标输出(用indexof())
				 	 var findStr = function () {
				 	 	var str = '今天是什么鬼天气，这么热，明天要去游泳！';
				 	 	var char_index = str.indexOf('天');
				 	 	var arr = [];
				 	 	while( char_index > -1 ) {
				 	 		arr.push(char_index);
				 	 		char_index = str.indexOf('天',char_index+1);				 	 		
				 	 	}
				 	 	console.log(arr.toString());
				 	 }();

			//5.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的有"天"替换成"日"(用replace())
					var replaceStr = function () {
						var str = '今天是什么鬼天气，这么热，明天要去游泳！';
						var newStr = str.replace(/天/g,'日');
						console.log(newStr);
					}();

					(function () {
						var str = '今天是什么鬼天气，这么热，明天要去游泳！';
						for ( var i = 0; i < str.length; i++ ) {
							if ( str.charAt(i) == '天' ){
								str = str.replace('天','日');
							}
						} 
						console.log(str);
					})();

					(function () {
						var str = '今天是什么鬼天气，这么热，明天要去游泳！';
							function replace(str,p,r){
								str = str.replace(p,r);
								if (str.indexOf(p) !== -1){
									return replace(str,p,r)
								}else{
									return str;
								}
							}
					console.log(replace(str,'天','日'));
					})();
			//6.移除数组 arr[1, 2, 3, 4, 2] 中的2。不要直接修改数组 arr，结果返回新的数组
					 var removeStr = function () {
					 	var arr = [1, 2, 3, 4, 2];
					 	var newStr = arr.toString().replace(/2/g,'');
					 	var newArr = newStr.split(',')
					 	var array = []
					 	for (var i in newArr){
					 		if ( newArr[i] != ''){
					 			array.push(newArr[i]);
					 		}
					 	}
					 	console.log(array);
					 }();

			//7.定义一个函数,能在数组 arr[1, 2, 3, 4, 2] 的 "2"后面添加元素 "Melon"。不要直接修改数组 arr，结果返回新的数组
					 var addStr = function () {
					 	var arr = [1, 2, 3, 4, 2];
					 	var newStr = arr.toString().replace(/2/g,'2Melon');
					 	var newArr = newStr.split(',')
					 	var array = []
					 	for (var i in newArr){
					 		if ( newArr[i] != ''){
					 			array.push(newArr[i]);
					 		}
					 	}
					 	console.log(array);
					 }();

			//8.统计数组 arr[1, 2, 3, 4, 2] 中2出现的次数
					var count = function () {
						var arr = [1, 2, 3, 4, 2];
						var repeat = 2;
						var timer = 0;
						var i = 0;
						while( i < arr.length ){
							if ( arr[i] == repeat ){
								timer++;
							}
							i++;
						}
						console.log(timer);
					}();

			//9.找出数组 arr[1, 2, 3, "melon", 4, "melon", 2, 4, "melon"] 中重复出现过的元素,并用数组将重复元素装起来输出
						var count = function () {
							var arr = [1, 2, 3, "melon", 4, "melon", 2, 4, "melon"]
							var repeat_arr = [];
							for ( var i = 0; i < arr.length; i++ ){
								for ( var j = i+1; j < arr.length; j++ ){
									if ( arr[i] == arr[j] && repeat_arr.indexOf(arr[j]) == -1){
										repeat_arr.push(arr[i]);
									}
								}
							}
							console.log(repeat_arr);
						}();
			//10.在arr里面输出年龄小于17的对象
			//			var arr = [
			//				{ name: "111", sex: "boy", age: 18 },
			//				{ name: "222", sex: "girl", age: 17 },
			//				{ name: "333", sex: "boy", age: 16 },
			//				{ name: "444", sex: "girl", age: 15 },
			//				{ name: "555", sex: "boy", age: 20 }
			//			]
			(function(){
				var arr = [
					{ name: "111", sex: "boy", age: 18 },
					{ name: "222", sex: "girl", age: 17 },
					{ name: "444", sex: "girl", age: 15 },
					{ name: "555", sex: "boy", age: 20 }
				]
				for ( var i in arr ) {
					arr[i].age < 17 ? console.log(arr[i].age) : '';
				}
			})();
		</script>
	</body>

</html>